/*
 * Copyright (C) 2024 HiHope Open Source Organization.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import ConfigData from '../../../../../../../common/utils/src/main/ets/default/baseUtil/ConfigData';
import type ISettingsController
  from '../../../../../../../common/component/src/main/ets/default/controller/ISettingsController';
import SwitchController from '../../../../../../../common/component/src/main/ets/default/controller/SwitchController';
import LogUtil from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil';
import { LogAll } from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogDecorator';
import oneConnectShare from '@ohos.oneConnect.fileShare';

const MODULE_TAG = ConfigData.TAG + '.oneConnectShareController -> ';
const BUNDLE_NAME = 'com.ohos.settings';

@LogAll
export default class OneConnectShareController extends SwitchController {

    private shareManager: oneConnectShare.ShareManager;
    private realStatus: boolean = false;

    onOneConnectShareSwitchChangeCallback: (isOn: boolean) => void = (isOn: boolean) => {
        LogUtil.info(MODULE_TAG + `on ShareStatusChange, status: ${JSON.stringify(isOn)}`);
        this.isOn = isOn;
        this.realStatus = this.isOn;
    }

    /**
     * Initialize data.
     */
    initData(): ISettingsController {
        super.initData();
        LogUtil.info(MODULE_TAG + `oneConnectShare module is ${oneConnectShare}`);
        this.shareManager = oneConnectShare?.createShareManager(BUNDLE_NAME);
        LogUtil.info(MODULE_TAG + `createShareManager result is ${this.shareManager}`);
        let isOn: boolean | undefined = this.shareManager?.getShareStatus();
        LogUtil.info(MODULE_TAG + `initData isOn = ${isOn}`);
        this.isOn = isOn || false;
        this.realStatus = this.isOn;
        return this;
    }
    /**
     * Subscribe listeners.
     */
    subscribe(): ISettingsController {
      LogUtil.info(MODULE_TAG + `subscribe in`);
      this.shareManager?.on('shareStatusChange', this.onOneConnectShareSwitchChangeCallback);
      AppStorage.set('shareOpenTime', this.getShareOpenTime());
      return this;
    };

    unsubscribe(): ISettingsController {
      LogUtil.info(MODULE_TAG + `unsubscribe in`);
      this.shareManager?.off('shareStatusChange');
      oneConnectShare?.releaseShareManager(this.shareManager);
      this.shareManager = undefined;
      return this;
    }

    /**
     * After current value changed event
     */
  afterCurrentValueChanged(): void {
    LogUtil.info(MODULE_TAG + 'afterCurrentValueChanged, ui isOn = ' + this.isOn);
    if (this.isOn == this.realStatus) {
      LogUtil.info(MODULE_TAG + ` shareStatus is already ${this.realStatus}, no need to change`);
      return;
    }
    if (this.isOn) {
      LogUtil.info(MODULE_TAG + `before enableShare`);
      this.shareManager?.enableShare();
    } else {
      LogUtil.info(MODULE_TAG + `before disableShare`);
      this.shareManager?.disableShare();
    };
  }

  getShareOpenTime(): number {
    LogUtil.info(MODULE_TAG + 'getShareOpenTime in');
    let time = this.shareManager?.getShareOpenTime();
    LogUtil.info(MODULE_TAG + 'getShareOpenTime result is ' + time);
    return time;
  }

  setShareOpenTime(time: number): void {
    LogUtil.info(MODULE_TAG + 'setShareOpenTime in, time = ' + time);
    this.shareManager?.setShareOpenTime(time);
    AppStorage.set('shareOpenTime', time);
  }
}